package com.infinitus.autocatlog.domain;

import java.util.Properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;


@Component
public class KafkaConfig {

	@Value("${clean.kafka.bootstrap.servers}")
	protected String servers;
	
	@Value("${clean.kafka.acks}")
	protected String acks;
	
	@Value("${clean.kafka.retries}")
	protected String retries;
	
	@Value("${clean.kafka.batch.size}")
	protected String size;
	
	@Value("${clean.kafka.linger.ms}")
	protected String ms;
	
	@Value("${clean.kafka.buffer.memory}")
	protected String memory;
	
	@Value("${clean.kafka.enable.auto.commit}")
	protected String autoCommit;
	
	@Value("${clean.kafka.auto.commit.interval.ms}")
	protected String commitIntervalms;
	
	@Value("${clean.session.timeout.ms}")
	protected String timeout;
	
	@Value("${clean.kafka.key.serializer}")
	protected String keySerializer;
	
	@Value("${clean.kafka.value.serializer}")
	protected String valueSerializer;
	
	@Value("${clean.kafka.key.deserializer}")
	protected String keyDeserializer;
	
	@Value("${clean.kafka.value.deserializer}")
	protected String valueDeserializer;
	
	@Value("${clean.kafka.group.id}")
	protected String groupId;
	
	
	
	
	@Value("${kafka.eventmetric.pro.topic}")
	protected String eventmetricTopic;
	
	@Value("${kafka.pc.test.topic}")
	protected String pcTestTopic;
	
	@Value("${kafka.pc.pro.topic}")
	protected String pcProTopic;
	
	@Value("${kafka.app.test.topic}")
	protected String appTestTopic;
	
	@Value("${kafka.app.pro.topic}")
	protected String appProTopic;
	
	@Value("${sugo.field.type.url}")
	protected String filedTypeUrl;
	
	@Value("${sugo.field.type.map}")
	protected String filedTypeMap;
	

	public String getFiledTypeUrl(String topic) {
		return String.format(filedTypeUrl, topic) ;
	}


	public String getFiledType(int type) {
		return JSONObject.parseObject(filedTypeMap).getString(type+"");
	}



	public String getEventmetricTopic() {
		return eventmetricTopic;
	}



	public String getPcTestTopic() {
		return pcTestTopic;
	}



	public String getPcProTopic() {
		return pcProTopic;
	}



	public String getAppTestTopic() {
		return appTestTopic;
	}



	public String getAppProTopic() {
		return appProTopic;
	}



	public Properties getProperties(boolean isConsumer){
		Properties props = new Properties();
		props.put("bootstrap.servers", servers);
		props.put("group.id", groupId);
		props.put("acks", acks);
		props.put("retries", retries);
		props.put("batch.size", size);
		props.put("linger.ms", ms);
		props.put("buffer.memory", memory);
		props.put("key.serializer", keySerializer);
		props.put("value.serializer", valueSerializer);
		props.put("key.deserializer", keyDeserializer);
		props.put("value.deserializer", valueDeserializer);
		if(isConsumer){
			props.put("enable.auto.commit", autoCommit);
	        props.put("auto.commit.interval.ms", commitIntervalms);
	        props.put("session.timeout.ms", timeout);
		}
		return props;
	}
	
}
